from selenium.common.exceptions import WebDriverException, NoSuchElementException

from utils import config
import datetime
import logging.config
import random
from utils.db import *
from utils.tools import *
from selenium.webdriver.support import expected_conditions as EC

import requests
import urllib.request
import os
from pyquery import PyQuery as pq
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

from utils.config import *
from selenium.webdriver.support.wait import WebDriverWait
# 日志
from os import path
log_file_path = path.join(path.dirname(path.abspath(__file__)), 'resources/china_stat.conf')
logging.config.fileConfig(log_file_path)
logger = logging.getLogger()
#数据库实例
mysql = mysql('table_carwler')

"""
根据text点击
"""
def by_text(brower, indicator):
    try:
        r = brower.find_element_by_link_text(indicator['text_name'])
    except  NoSuchElementException as e:
        brower.execute_script('window.scrollTo(0,document.body.scrollHeight)')
        r = brower.find_element_by_link_text(indicator['text_name'])

    id = r.get_attribute("id")
    print(id)
    #mouse = brower.find_element_by_id(id)
    #action =  ActionChains(brower).move_to_element(mouse)
    #action.click()
    try:
        brower.find_element_by_id(id).click()
    except WebDriverException as e:
        brower.execute_script('window.scrollTo(0,document.body.scrollHeight)')
        brower.find_element_by_id(id).click()

    wait=WebDriverWait(brower,10)#等待元素加载出来
    wait.until(EC.presence_of_element_located((By.ID,id)))
    return id




def by_other(brower, indicator):
    print("使用其他方式运行")
    type = indicator['attr_type']
    if not type:
        logger.error("属性类型是空的，以text方式进行")
        by_text(brower,indicator)
    elif type=='xpath':

        brower.find_element(By.XPATH,indicator['craw_attr']).click()
    elif type=='id':

        brower.find_element(By.ID,indicator['craw_attr']).click()
    else:
        by_text(brower,indicator)


"""
判断以哪种方式点击
"""
def click_id_by_text(brower, indicator):
    try:
        if not indicator['craw_attr'] :
            return by_text(brower,indicator)
        else:
            return by_other(brower,indicator)
    except NoSuchElementException as e:
        logger.error("得不到ID根据Text",indicator['craw_attr'],indicator['Id'])

"""层级函数，主函数"""
def china_stat_tier_function(pid=0):
    print('父ID'+str(pid))
    indicatorList =  query_list(pid)
    if not indicatorList:
        logger.error("指标数据为空")
        return "指标数据为空"
    options = webdriver.ChromeOptions()
    options.binary_location = binary_location #谷歌地址
    #driver地址
    brower = webdriver.Chrome(chrome_options=options)

    for indicator in indicatorList:
        indicator['text_name']
        print('循环中'+str(indicator['Id']))
        #打开网页
        brower.get(indicator['url'])
        id = click_id_by_text(brower,indicator)
        #在此处查询第二层
        tier = indicator['tier_s']
        if(tier==2):
            end_function(indicator,brower)

        elif(tier==3):
            end_function3(indicator,brower)

"""SQL封装"""
def query_list(param):
    cur = mysql.conn.cursor()
    mysql.field('*')
    mysql.where(" p_id="+str(param)+" and is_used=0  ")
    mysql.order_sql = " order by Id"

    indicatorList = mysql.find(0)
    return indicatorList

"""无子层调用函数"""
def end_function(indicator,brower):
    print("最后一层")
    indicatorList2 =  query_list(indicator['Id'])

    for indicator2 in indicatorList2:
        time.sleep(1)
        print(indicator2['text_name'])
        id = click_id_by_text(brower,indicator2)

        result = get_table_content('table_main',brower)
        data =  analyize_data(result,indicator2)
        message = urllib_post(config.china_stat_url,data)
        print(message)






"""调用接口封装数据"""
def analyize_data(result, indicator):

    data = {'pIndicator':indicator['map_name'],
            'groupCode':indicator['group_code'],
            'area_name':'全国',
            'data':result}

    return data

"""取出列表数据，此处只根据1种网站实现，可模板化"""
def get_table_content(tableId,brower):
    arr = []
    arr1 = []
    table_loc = (By.ID,tableId)
    # 按行查询表格的数据，取出的数据是一整行，按空格分隔每一列的数据
    table_tr_list = brower.find_element(*table_loc).find_elements(By.TAG_NAME, "tr")
    for tr in table_tr_list:
        print(tr.text)
        arr1 = (tr.text).split(" ") #以空格拆分成若干个(个数与列的个数相同)一维列表
        arr.append(arr1)    #将表格数据组成二维的列表
    return arr

"""
指定层数，3层抓取
"""
def end_function3(indicator,brower):
    print("三层抓取")

    indicatorList =  query_list(indicator['Id'])
    for indicator2 in indicatorList:
        print(indicator2['text_name'])
        id = click_id_by_text(brower,indicator2)
        end_function(indicator2,brower)



if __name__ == '__main__':
   r =  china_stat_tier_function(0)
   data = {'pIndicator':'44444',
           'groupCode':'3333',
           'area_name':'全国',
           'data':'1111'}
  # message = urllib_post(config.china_stat_url,data)
  # print(message)